#include "mainwindow.h"
#include "ui_mainwindow.h"



MainWindow::MainWindow(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->setWindowFlag(Qt::FramelessWindowHint);

    connect(this,&MainWindow::index,this,&MainWindow::pushButton_skin);

    //logo展示
    ui->weixin_logo->setStyleSheet("background-color:rgba(0,0,0,0)");

    //好友列表添加测试
    ui->listWidget->addFriend(":/new/prefix1/img/YY.jpg", "暮云沉沉，烟雨成空", "今天晚上吃什么呢？", "24/7/10","1");
    ui->listWidget->addFriend(":/new/prefix1/img/YY.jpg", "晓风残月", "还好吗？", "昨天","2");
    ui->listWidget->addFriend(":/new/prefix1/img/YY.jpg", "冬雪映春", "我们聊一下新项目吧。", "25/7/9","3");
    ui->listWidget->addFriend(":/new/prefix1/img/YY.jpg", "夏日清风", "今晚有空聚一聚。", "今天","9");
    ui->listWidget->addFriend(":/new/prefix1/img/YY.jpg", "暮云沉沉，烟雨成空", "今天晚上吃什么呢？", "24/7/10","4");
    ui->listWidget->addFriend(":/new/prefix1/img/YY.jpg", "晓风残月", "还好吗？", "昨天","5");
    ui->listWidget->addFriend(":/new/prefix1/img/YY.jpg", "冬雪映春", "我们聊一下新项目吧。", "25/7/9","6");

    //初始化托盘图标
    trayIcon=new QSystemTrayIcon(this);
    trayIcon->setIcon(QIcon(":/new/prefix1/img/YY.jpg"));//设置托盘图标
    trayIcon->setToolTip("WeChat Clone");//托盘提示

    //创建托盘菜单
    trayMenu=new QMenu(this);
    QAction *showAction=new QAction("显示",this);
    QAction *exitAction=new QAction("退出",this);

    //连接菜单项的槽函数
    connect(showAction,&QAction::triggered,this, &MainWindow::onShowActionTriggered);
    connect(exitAction, &QAction::triggered, this, &MainWindow::onExitActionTriggered);

    // 添加菜单项
    trayMenu->addAction(showAction);
    trayMenu->addAction(exitAction);

    // 设置托盘菜单
    trayIcon->setContextMenu(trayMenu);

    // 连接托盘图标的点击事件
    connect(trayIcon, &QSystemTrayIcon::activated, this, &MainWindow::onTrayIconActivated);

    // 显示托盘图标
    trayIcon->show();

    // 连接 vx_listwidget 的 cellClicked 信号
    connect(ui->listWidget, &vx_listwidget::cellClicked, this, &MainWindow::onCellClicked);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_close_clicked()
{
    this->close();
}

void MainWindow::on_pushButton_1_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
    emit index(0);
}

void MainWindow::on_pushButton_2_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
    emit index(1);
}

void MainWindow::on_pushButton_3_clicked()
{
    ui->stackedWidget->setCurrentIndex(2);
    emit index(2);
}

void MainWindow::on_pushButton_4_clicked()
{
    ui->stackedWidget->setCurrentIndex(3);
    emit index(3);
}

void MainWindow::on_pushButton_5_clicked()
{
    ui->stackedWidget->setCurrentIndex(4);
    emit index(4);
}

void MainWindow::on_pushButton_6_clicked()
{
    ui->stackedWidget->setCurrentIndex(5);
    emit index(5);
}

void MainWindow::on_pushButton_7_clicked()
{
    ui->stackedWidget->setCurrentIndex(6);
    emit index(6);
}

void MainWindow::on_pushButton_8_clicked()
{
    ui->stackedWidget->setCurrentIndex(7);
    emit index(7);
}

void MainWindow::on_pushButton_9_clicked()
{
    ui->stackedWidget->setCurrentIndex(8);
    emit index(8);
}

void MainWindow::on_pushButton_10_clicked()
{
    ui->stackedWidget->setCurrentIndex(9);
    emit index(9);
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    // 在鼠标按下时记录当前位置
    if (event->button() == Qt::LeftButton) {
        dragPosition = event->globalPos() - frameGeometry().topLeft();
        event->accept();
    }
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    // 在鼠标移动时移动窗口
    if (event->buttons() & Qt::LeftButton) {
        move(event->globalPos() - dragPosition);
        event->accept();
    }
}

void MainWindow::pushButton_skin(int i)
{
    ui->pushButton_1->setStyleSheet("background-image:url(':/new/prefix1/img/unchat.png');");
    ui->pushButton_2->setStyleSheet("background-image:url(':/new/prefix1/img/unpeople.png');");
    ui->pushButton_3->setStyleSheet("background-image:url(':/new/prefix1/img/uncolection.png');");
    ui->pushButton_4->setStyleSheet("background-image:url(':/new/prefix1/img/unfile.png');");
    ui->pushButton_5->setStyleSheet("background-image:url(':/new/prefix1/img/unfriend.png');");
    ui->pushButton_6->setStyleSheet("background-image:url(':/new/prefix1/img/unsee.png');");
    ui->pushButton_7->setStyleSheet("background-image:url(':/new/prefix1/img/unresearch.png');");
    ui->pushButton_8->setStyleSheet("background-image:url(':/new/prefix1/img/unsp.png');");
    ui->pushButton_9->setStyleSheet("background-image:url(':/new/prefix1/img/unphone.png');");
    ui->pushButton_10->setStyleSheet("background-image:url(':/new/prefix1/img/unsetting.png');");
    switch(i)
    {
    case 0:
    {
        ui->pushButton_1->setStyleSheet("background-image:url(':/new/prefix1/img/chat.png');");
    }
        break;
    case 1:
    {
        ui->pushButton_2->setStyleSheet("background-image:url(':/new/prefix1/img/people.png');");
    }
        break;
    case 2:
    {
        ui->pushButton_3->setStyleSheet("background-image:url(':/new/prefix1/img/colection.png');");
    }
        break;
    case 3:
    {
        ui->pushButton_4->setStyleSheet("background-image:url(':/new/prefix1/img/file.png');");
    }
        break;
    case 4:
    {
        ui->pushButton_5->setStyleSheet("background-image:url(':/new/prefix1/img/friend.png');");
    }
        break;
    case 5:
    {
        ui->pushButton_6->setStyleSheet("background-image:url(':/new/prefix1/img/see.png');");
    }
        break;
    case 6:
    {
        ui->pushButton_7->setStyleSheet("background-image:url(':/new/prefix1/img/research.png');");
    }
        break;
    case 7:
    {
        ui->pushButton_8->setStyleSheet("background-image:url(':/new/prefix1/img/sp.png');");
    }
        break;
    case 8:
    {
        ui->pushButton_9->setStyleSheet("background-image:url(':/new/prefix1/img/phone.png');");
    }
        break;
    case 9:
    {
        ui->pushButton_10->setStyleSheet("background-image:url(':/new/prefix1/img/setting.png');");
    }
        break;
    }
}

void MainWindow::on_pushButton_add_clicked()
{

}

void MainWindow::onTrayIconActivated(QSystemTrayIcon::ActivationReason reason) {
    if (reason == QSystemTrayIcon::DoubleClick) {
        this->show();  // 双击托盘图标时显示主窗口
    }
}

void MainWindow::onShowActionTriggered() {
    this->show();  // 显示主窗口
}

void MainWindow::onExitActionTriggered() {

    trayIcon->hide();  // 隐藏托盘图标
    QApplication::quit();  // 退出程序

}

void MainWindow::closeEvent(QCloseEvent *event) {
    if (trayIcon->isVisible()) {
        this->hide();  // 隐藏主窗口
        event->ignore();  // 忽略关闭事件
    }
}

void MainWindow::onCellClicked(const QString& friendId)
{
    // 在这里处理 cell 被点击的逻辑
    // 创建 chat_interface 实例并添加到 chatGroupBox

    if(chatWidget)
    {
        delete chatWidget;
        chatWidget=nullptr;
    }

    chatWidget = new chat_interface(friendId);
    // 将 chat_interface 添加到 chatGroupBox 中
    // 注意：这里需要将 chatWidget 作为子部件添加到 chatGroupBox 中，而不是直接添加到布局中
    ui->verticalLayout_chat->addWidget(chatWidget);
}
